<?php
namespace app\home\controller;

use app\common\model\Fu as FuModel;
use app\common\model\MemberQuan as MemberQuanModel;
use app\common\model\McheckQuan as McheckQuanModel;
/**
 * 福券控制器
 * Class Fu
 * @package app\home\controller
 */
class Fu extends Base
{
    /**
     * [getIndex  查看]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $mmodel = new MemberQuanModel();
        $mcmodel = new McheckQuanModel();
        $mem = $this->getMember($param['token']);
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['member_id'] = $mem['id'];
            $count = $mmodel->getCountByCondition($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $mmodel->getlists($map,$this->from,$this->size);
            foreach($lists as $k=>$v){
                $mcheck = $mcmodel->findData($v['member_quan_id'],'qid','status');
                if(!empty($mcheck)){
                    $lists[$k]['status'] = $mcheck['status'];
                }else{
                    $lists[$k]['status'] = 2;
                }
                $lists[$k]['addtime'] = date('Y-m-d',$v['addtime']);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postExchange  兑换]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postExchange()
    {
        $param = input('param.');
        $model = new FuModel();
        $mmodel = new MemberQuanModel();
        $mem = $this->getMember($param['token']);
        try{
            $code = $model->findData($param['code'],'code');
            if(empty($code)){
                return admin_json(2, '兑换码错误');
            }
            if($code['status'] != 1){
                return admin_json(3, '兑换码已兑换');
            }
            if($code['type'] == '五次'){
                $addtime = time();
                $list = [
                    ['member_id'=>$mem['id'],'fu_id'=>$code['fu_id'],'addtime'=>$addtime,'num'=>1,'jing'=>'普陀山'],
                    ['member_id'=>$mem['id'],'fu_id'=>$code['fu_id'],'addtime'=>$addtime,'num'=>1,'jing'=>'九华山'],
                    ['member_id'=>$mem['id'],'fu_id'=>$code['fu_id'],'addtime'=>$addtime,'num'=>1,'jing'=>'峨眉山'],
                    ['member_id'=>$mem['id'],'fu_id'=>$code['fu_id'],'addtime'=>$addtime,'num'=>1,'jing'=>'五台山'],
                    ['member_id'=>$mem['id'],'fu_id'=>$code['fu_id'],'addtime'=>$addtime,'num'=>1,'jing'=>'雪窦山'],
                ];
                $id_arr = $mmodel->addDataAll($list);
                if(!empty($id_arr)){
                    $model->edit(['fu_id'=>$code['fu_id'],'status'=>2]);
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '兑换失败'.$e->getMessage());
        }
        return admin_json(1, '兑换成功',$id_arr);
    }
    /**
     * [postCheck  提交审核]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postCheck()
    {
        $param = input('param.');
        $model = new McheckQuanModel();
        $mmodel = new MemberQuanModel();
        try{
            if(!isMobile($param['tel'])){
                return admin_json(3, '手机号有误');
            }
            if(!is_idcard($param['idcard'])){
                return admin_json(4, '身份证有误');
            }
            $check = $model->findData($param['qid'],'qid');
            $quan = $mmodel->findData($param['qid'],'member_quan_id','num');
            if(!empty($check)){
                if($check['status'] == -1){
                    $num = $quan['num'] - 1;
                    $mmodel->editNum(['member_quan_id'=>$param['qid'],'num'=>$num]);
                    $model->changeStatus(['id'=>$check['id']]);
                }else{
                    return admin_json(2, '已提交审核');
                }
            }else{
                $data = array(
                    'tel' => $param['tel'],
                    'realname' => $param['realname'],
                    'idcard' => $param['idcard'],
                    'qid' => $param['qid'],
                    'liketime' => strtotime($param['liketime']),
                    'sex' => $param['sex'],
                );
                $model->addData($data);
                $num = $quan['num'] - 1;
                $mmodel->editNum(['member_quan_id'=>$param['qid'],'num'=>$num]);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '提交失败');
        }
        return admin_json(1, '提交成功');
    }
    /**
     * [getSuccess  获取审核成功]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getSuccess()
    {
        $param = input('param.');
        $model = new McheckQuanModel();
        try{
            $mckeck = $model->findData($param['qid'],'qid','img,usetime');
            $mckeck['usetime'] = date('Y-m-d',$mckeck['usetime']);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败');
        }
        return admin_json(1, '获取成功',$mckeck);
    }
    /**
     * [getMess  获取审核中用户信息]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getMess()
    {
        $param = input('param.');
        $model = new McheckQuanModel();
        try{
            $mckeck = $model->findData($param['qid'],'qid','realname,tel,idcard,status,liketime,sex');
            $mckeck['liketime'] = date('Y-m-d',$mckeck['liketime']);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败');
        }
        return admin_json(1, '获取成功',$mckeck);
    }
}